Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-82 TransportContext: merge and key iterator #85

Merged
merged 2 commits into from
Aug 13, 2024
Merged

Conversation

szysas
Copy link
Collaborator

@szysas szysas commented Apr 23, 2024

No description provided.

@szysas szysas requested a review from sbernard31 April 23, 2024 12:09
@sbernard31
Copy link
Collaborator

sbernard31 commented Apr 23, 2024

It's take me few minutes to get how "merge with override" works. 🤔
Actually, when you merge if there is 2 value with same key both are kept in the chained list but only the first one will be returned by the get(), correct ?

That's surprising me but if it works why not. I guess one drawback is that merged context takes probably a bit more memory than necessary.

I didn't test it but reading the code I guess that :

TransportContext ctx1 = TransportContext.of(DUMMY_KEY, "111").with(DUMMY_KEY2, "222");
TransportContext ctx2 = TransportContext.of(DUMMY_KEY, "aaa");
TransportContext ctx1Ctx2 = ctx1.with(ctx2);

TransportContext ctx3 = TransportContext.of(DUMMY_KEY, "aaa").with(DUMMY_KEY2, "222");   
// OR TransportContext ctx3 = TransportContext.of(DUMMY_KEY2, "222").with(DUMMY_KEY, "aaa");  
// not sure about the merge order : this makes me think 
// that maybe a better name would be addFirst() or addLast() (instead of with())
// Just because order seems relevant for equals()

assertEquals(ctx1Ctx2, ctx3); 
// From user point of view, I guess this should be equal, but is it ? 
// reading the code I'm not sure. 

and also if I iterate over ctx1Ctx2 and print all key, I will get DUMMY_KEY twice correct ?

(I maybe totally missed something, sorry I didn't take time to execute code)

@szysas
Copy link
Collaborator Author

szysas commented Apr 24, 2024

and also if I iterate over ctx1Ctx2 and print all key, I will get DUMMY_KEY twice correct ?

Yes, that is correct.

@szysas szysas force-pushed the merge-context branch 2 times, most recently from 8169ac0 to fb56170 Compare July 10, 2024 17:38
@szysas
Copy link
Collaborator Author

szysas commented Jul 10, 2024

and also if I iterate over ctx1Ctx2 and print all key, I will get DUMMY_KEY twice correct ?

@sbernard31 Now that it is fixed by returning Set with keys.

@sbernard31
Copy link
Collaborator

I looked at this.
The keys set resolve the "twice" error.

But not the "equals" one ? See code comment in code block of #85 (comment).

Copy link

codecov bot commented Aug 6, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 93.15%. Comparing base (ef8b0e2) to head (0d78208).

Additional details and impacted files
@@             Coverage Diff              @@
##             master      #85      +/-   ##
============================================
+ Coverage     93.11%   93.15%   +0.03%     
- Complexity     1992     1998       +6     
============================================
  Files           132      132              
  Lines          4505     4513       +8     
  Branches        616      616              
============================================
+ Hits           4195     4204       +9     
  Misses          168      168              
+ Partials        142      141       -1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@szysas
Copy link
Collaborator Author

szysas commented Aug 6, 2024

I looked at this. The keys set resolve the "twice" error.
I'm not sure what do you mean.

But not the "equals" one ? See code comment in code block of #85 (comment).

Well, that is true, equals will not be true.

@sbernard31
Copy link
Collaborator

(let me know if I should review this again ?)

@szysas
Copy link
Collaborator Author

szysas commented Aug 12, 2024

(let me know if I should review this again ?)

Yes, please ;)

@@ -66,6 +68,17 @@ public <T> TransportContext with(Key<T> key, T value) {
return new TransportContext(requireNonNull(key), requireNonNull(value), this);
}

public TransportContext with(TransportContext otherCtx) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe we should add some javadoc to explain that :
If a key exists in both contexts, the otherCtx value will be used.

@sbernard31
Copy link
Collaborator

sbernard31 commented Aug 12, 2024

I'm still a bit confused by the idea that a not used key/value pair should still be stored in memory 😅 (#85 (comment))

Actually, when you merge if there is 2 value with same key both are kept in the chained list but only the first one will be returned by the get(), correct ?
That's surprising me but if it works why not. I guess one drawback is that merged context takes probably a bit more memory than necessary.

But alternative will probably lead to more costly(and complex) way to merge 2 TransportContext, so maybe this is the good way. 🤷

So this looks good to me.

@szysas
Copy link
Collaborator Author

szysas commented Aug 13, 2024

Purpose for TransportContext is to have short living, simple and small object. When we will see that this is not enough then alternative would be to use some HashMap underneath.

@szysas szysas merged commit f4adb67 into master Aug 13, 2024
6 checks passed
@szysas szysas deleted the merge-context branch August 13, 2024 05:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants